package com.springboot.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.config.log.SpringBootRequestLog;
import com.springboot.config.controller.BaseController;
import com.springboot.returndata.dto.*;
import com.springboot.returndata.vo.*;
import com.springboot.service.system.SystemMenuService;
import com.springboot.service.system.SystemRoleMenuService;
import com.springboot.service.system.SystemRoleService;
import com.springboot.service.system.SystemUserRoleService;
import com.springboot.service.system.UserLoginService;
import com.springboot.util.enums.RespErrorCode;
import com.springboot.util.http.ResponseBuilder;
import com.springboot.util.http.ResultResponse;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;
import java.util.Map;

/**
 * description: 系统设置相关
 *
 * @author: zyj
 * @date: 2023/2/22
 */
@Slf4j
@RestController
@RequestMapping("/system")
@SpringBootRequestLog
@Tag(name = "系统设置相关")
public class SystemController extends BaseController {

    @Resource
    private SystemMenuService systemMenuService;

    @Resource
    private SystemRoleService systemRoleService;

    @Resource
    private SystemRoleMenuService systemRoleMenuService;

    @Resource
    private SystemUserRoleService systemUserRoleService;

    @Resource
    private UserLoginService userLoginService;

    @GetMapping("/getAllMenu")
    @Operation(description = "获取所有菜单按钮")
    public ResultResponse<ResultVo> getAllMenu(String type) {
        try {
            List allMenuByType = systemMenuService.getAllMenuByType(type);
            ResultVo resultVo = new ResultVo();
            resultVo.setHomeInfo(systemMenuService.shoye());
            resultVo.setMenuInfo(allMenuByType);
            return ResponseBuilder.successData(resultVo);
        } catch (Exception e) {
            log.info("获取所有菜单按钮 失败：", e);
        }

        return null;
    }

    @GetMapping("/getShouyeInfo")
    @Operation(description = "获取首页信息")
    public ResultResponse<ResultVo.HomeInfo> getShouyeInfo() {
        try {
            return ResponseBuilder.successData(systemMenuService.shoye());
        } catch (Exception e) {
            log.info("获取所有菜单按钮 失败：", e);
        }
        return null;
    }

    @PostMapping("/saveShouyeInfo")
    @Operation(description = "保存首页信息")
    public ResultResponse<String> saveShouyeInfo(@RequestBody Map<String, String> shouye) {
        try {
            systemMenuService.saveShoye(shouye);
            return ResponseBuilder.success();
        } catch (Exception e) {
            log.info("获取所有菜单按钮 失败：", e);
        }
        return null;
    }

    @GetMapping("/getResourceMenu")
    @Operation(description = "获取所有菜单按钮")
    public ResultResponse<ResourceMenuVo> getResourceMenu(){
        try {
            List<ResourceMenuVo> allResourceMenu = systemMenuService.getAllResourceMenu();
            return ResponseBuilder.successData(allResourceMenu);
        } catch (Exception e) {
            log.info("获取所有菜单按钮 失败：", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());
    }

    @GetMapping("/getMenuAuthorityByCode")
    @Operation(description = "获取所有权限标志")
    public ResultResponse<String> getMenuAuthorityByCode(String menuCode){
        try {
            List<String> allResourceMenu = systemMenuService.getMenuAuthorityByCode(menuCode, getUserId());
            return ResponseBuilder.successData(allResourceMenu);
        } catch (Exception e) {
            log.info("获取所有权限标志 失败：{}", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());
    }

    @PostMapping("/getRoleList")
    @Operation(description = "获取用户角色列表")
    public ResultResponse<Page<SystemRoleVO>> getRoleList(@RequestBody GetRoleListDto getRoleListDto){

        try {
            Page<SystemRoleVO> roleList = systemRoleService.getRoleList(getRoleListDto);
            return ResponseBuilder.successData(roleList);
        }catch (Exception e){
            log.error("getRoleList error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());
    }


    @GetMapping("/getRoleListAll")
    @Operation(description = "获取用户角色列表")
    public ResultResponse<SystemRoleVO> getRoleListAll(){

        try {
            List<SystemRoleVO> roleList = systemRoleService.getRoleListAll();
            return ResponseBuilder.successData(roleList);
        }catch (Exception e){
            log.error("getRoleList error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());
    }


    @GetMapping("/getRoleResourceTree")
    @Operation(description = "获取用户权限树列表")
    public ResultResponse<RoleResourceVo> getRoleResourceTree(Integer roleId){

        try {
            List<RoleResourceVo> roleResourceVo = systemRoleService.getRoleResourceTree(roleId);
            return ResponseBuilder.successData(roleResourceVo);
        }catch (Exception e){
            log.error("getRoleList error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());
    }

    @PostMapping("/saveRoleResourceTree")
    @Operation(description = "保存用户权限树列表")
    public ResultResponse<String> saveRoleResourceTree(@Valid @RequestBody SaveRoleResourceTreeDto saveRoleResourceTreeDto){

        try {
            systemRoleMenuService.saveRoleResourceTree(saveRoleResourceTreeDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("getRoleList error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());
    }

    @PostMapping("/updateResourceMenu")
    @Operation(description = "更新资源")
    public ResultResponse<String> updateResourceMenu(@RequestBody AddUpdateResourceDto addUpdateResourceDto){
        try {
           systemMenuService.updateResourceMenu(addUpdateResourceDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("updateResourceMenu error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }

    @PostMapping("/addResourceMenu")
    @Operation(description = "添加资源")
    public ResultResponse<String> addResourceMenu(@Valid @RequestBody AddUpdateResourceDto addUpdateResourceDto){
        try {
            systemMenuService.addResourceMenu(addUpdateResourceDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("updateResourceMenu error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }

    @GetMapping("/deleteResourceMenu")
    @Operation(description = "删除资源")
    public ResultResponse<String> deleteResourceMenu(@RequestParam() Integer id, @RequestParam() String menuCode){
        try {
            systemMenuService.deleteResourceMenu(id, menuCode);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("deleteResourceMenu error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }


    @PostMapping("/getUserList")
    @Operation(description = "获取用户列表")
    public ResultResponse<Page<UserLoginVo>> getUserList(@RequestBody GetUserListDto userListDto){

        try {
            Page<UserLoginVo> roleList = userLoginService.getUserList(userListDto);
            return ResponseBuilder.successData(roleList);
        }catch (Exception e){
            log.error("getRoleList error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());
    }


    @PostMapping("/addRole")
    @Operation(description = "添加角色")
    public ResultResponse<String> addRole(@Valid @RequestBody AddUpdateRoleDto addUpdateResourceDto){
        try {
            systemRoleService.addRole(addUpdateResourceDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("addRole error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }


    @PostMapping("/updateRole")
    @Operation(description = "修改角色")
    public ResultResponse<String> updateRole(@Valid @RequestBody AddUpdateRoleDto addUpdateResourceDto){
        try {
            systemRoleService.updateRole(addUpdateResourceDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("updateRole error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }



    @GetMapping("/deleteRole")
    @Operation(description = "删除角色")
    public ResultResponse<String> deleteRole(Integer roleId){
        try {
            systemRoleService.deleteRole(roleId);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("deleteRole error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }

    @PostMapping("/addUpdateUserRole")
    @Operation(description = "添加资源")
    public ResultResponse<String> addUpdateUserRole(@Valid @RequestBody AddUpdateUserRoleDto addUpdateResourceDto){
        try {
            systemUserRoleService.addUpdateUserRole(addUpdateResourceDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("updateResourceMenu error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }




    @PostMapping("/addUser")
    @Operation(description = "添加用户")
    public ResultResponse<String> addUser(@Valid @RequestBody AddUpdateUserDto addUpdateUserDto){
        try {
            userLoginService.addUser(addUpdateUserDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("addUser error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }


    @PostMapping("/updateUser")
    @Operation(description = "修改用户")
    public ResultResponse<String> updateUser(@Valid @RequestBody AddUpdateUserDto addUpdateUserDto){
        try {
            userLoginService.updateUser(addUpdateUserDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("updateUser error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }



    @GetMapping("/deleteUser")
    @Operation(description = "删除用户")
    public ResultResponse<String> deleteUser(@RequestParam(required = true) Integer userId){
        try {
            userLoginService.deleteUser(userId);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("deleteUser error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }
}
